package com.j48.entrancegurad.service.impl;

import com.j48.entrancegurad.mapper.PictureMapper;
import com.j48.entrancegurad.model.Picture;
import com.j48.entrancegurad.service.IPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class PictureService implements IPictureService {
    @Autowired
    private PictureMapper pictureMapper;

    /**
     * 查询全部图片
     * @return
     */
    @Override
    public List<Picture> selectAll() {
        List<Picture> lists=new ArrayList<Picture>();
        lists=pictureMapper.selectAll();
        if (lists.size()>0){
            return lists;
        }
        return null;
    }

    /**
     * 查询全部可见图片
     * @return
     */
    @Override
    public List<Picture> selectAllAble() {
        List<Picture> lists=new ArrayList<Picture>();
        lists=pictureMapper.selectAllAble();
        if (lists.size()>0){
            return lists;
        }
        return null;
    }

    /**
     * 删除图片
     * @param picture
     * @return
     */
    @Override
    public boolean delete(Picture picture) {
        if(pictureMapper.delete(picture)>0){
            return true;
        }else{
            return  false;
        }
    }

    /**
     * 更新图片
     * @param picture
     * @return
     */
    @Override
    public boolean update(Picture picture) {
        if(pictureMapper.update(picture)>0){
            return true;
        }else{
            return  false;
        }
    }

    /**
     * 上传图片
     * @param picture_id
     * @param file
     * @return
     */
    @Override
    public String uploadImg(HttpServletRequest request,int picture_id,MultipartFile file) {
        if (null != file) {
            String myFileName = file.getOriginalFilename();// 文件原名称
            String fileName =
                    Integer.toHexString(new Random().nextInt()) +"."+ myFileName.
                    substring(myFileName.lastIndexOf(".") + 1);
            String path=null;
            String realPath = request.getSession().getServletContext().getRealPath("/");
            System.out.println();
            String trueFileName =fileName;
            String os=System.getProperty("os.name");
            // 设置存放图片文件的路径
            if(os.toLowerCase().startsWith("win")){
                path = "D:/static/"+/*System.getProperty("file.separator")+*/trueFileName;
            }else {
                File mir=new File("/static/");
                if(!mir.exists()){
                    mir.mkdirs();
                }
                path="/static/"+trueFileName;
            }
            File localFile = new File(path);
            try {
                file.transferTo(localFile);
                Picture picture=new Picture();
                picture.setPicture_id(picture_id);
                picture.setPicture_url(fileName);
                if(pictureMapper.update(picture)>0){
                    return fileName;
                }
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else{
            System.out.println("文件为空");
        }
        return null;
    }

    /**
     * 上传图片
     * @param file
     * @param pat
     * @return
     */
    @Override
    public String addPicture(MultipartFile file,String pat) {
        if (null != file) {
            String myFileName = file.getOriginalFilename();// 文件原名称
            String fileName =
                    Integer.toHexString(new Random().nextInt()) +"."+ myFileName.
                            substring(myFileName.lastIndexOf(".") + 1);

            String sqlPath="/WEB-INF/static/images/";
            File fileDir=new File(pat+sqlPath);
            if (!fileDir.exists()) { //如果不存在 则创建
                fileDir.mkdirs();
            }
            String path=pat+sqlPath+fileName;
            File localFile = new File(path);
            try {
                file.transferTo(localFile);
                Picture picture=new Picture();
                picture.setPicture_url(fileName);
                picture.setPicture_createtime(new Timestamp(new Date().getTime()));
                if(pictureMapper.insert(picture)>0){
                    return sqlPath + fileName;
                }
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else{
            System.out.println("文件为空");
        }
        return null;
    }
}
